package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public int save(Comment comment) {
        comment.setId(ObjectId.get());
        comment.setCreated(System.currentTimeMillis());
        mongoTemplate.insert(comment);

        if (comment.getPubType()==1){
            //对动态进行操作
            if (comment.getCommentType()==1){
                //对动态点赞
                return findAndModify(comment.getPublishId(), "likeCount", 1);

            }else if (comment.getCommentType()==2){
                //对动态进行评论
                return findAndModify(comment.getPublishId(), "commentCount", 1);
            }else if (comment.getCommentType()==3){
                //对动态进行喜欢
                return findAndModify(comment.getPublishId(), "loveCount", 1);
            }

        }else if (comment.getPubType()==2){
            // 对视频进行操作
            if (comment.getCommentType()==1){
                //对视频点赞
                return findAndModifyVideo(comment.getPublishId(), "likeCount", 1);
            }else if(comment.getCommentType()==2){
                return findAndModifyVideo(comment.getPublishId(), "commentCount", 1);

            }

        }else if (comment.getPubType()==3){
            //todo 对评论进行操作
        }
        return 0;
    }

    /**
     * 取消点赞操作
     * @param comment
     * @return
     */
    @Override
    public int remove(Comment comment) {
        Query query = new Query();
        query.addCriteria(Criteria.where("publishId").is(comment.getPublishId())
                                    .and("pubType").is(comment.getPubType())
                                    .and("commentType").is(comment.getCommentType())
                                    .and("userId").is(comment.getUserId()));

        mongoTemplate.remove(query,Comment.class);
        if (comment.getPubType()==1){
            //对动态进行操作
            if (comment.getCommentType()==1){
                //对动态点赞
                return findAndModify(comment.getPublishId(), "likeCount", -1);

            }else if (comment.getCommentType()==2){
                //对动态进行评论
                return findAndModify(comment.getPublishId(), "commentCount", -1);
            }else if (comment.getCommentType()==3){
                //对动态进行喜欢
                return findAndModify(comment.getPublishId(), "loveCount", -1);
            }

        }else if (comment.getPubType()==2){
            // 对视频进行操作
            if (comment.getCommentType()==1){
                //对视频取消点赞
                return findAndModifyVideo(comment.getPublishId(), "likeCount", -1);
            }else if(comment.getCommentType()==2){
                return findAndModifyVideo(comment.getPublishId(), "commentCount", -1);

            }

        }else if (comment.getPubType()==3){
            //todo 对评论进行操作
        }
        return 0;
    }

    @Override
    public PageResult findComments(String movementId, Integer page, Integer pagesize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(2));
        query.with(Sort.by(Sort.Order.desc("created")));

        query.with(PageRequest.of(page-1,pagesize));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        int pages = (int) Math.ceil(count*1.0/pagesize);
        return new PageResult<Comment>((int) count,pagesize,pages,page,comments);
    }

    @Override
    public PageResult commentList(Integer page, Integer pagesize, Long userId, int commentType) {
        Query query = new Query();
        query.addCriteria(Criteria.where("commentType").is(commentType).and("publishUserId").is(userId));
        query.with(Sort.by(Sort.Order.desc("created")));
        query.with(PageRequest.of(page-1,pagesize));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        int pages = (int) Math.ceil(count*1.0/pagesize);
        return new PageResult((int) count,pagesize,pages,page,comments);
    }


    public int findAndModify(ObjectId publishId,String countName,int inc){
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(publishId));
        Update update = new Update();
        update.inc(countName,inc);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Publish publish = mongoTemplate.findAndModify(query, update, options, Publish.class);
        if ("likeCount".equals(countName)){
            return publish.getLikeCount();
        }else if ("loveCount".equals(countName)){
            return publish.getLoveCount();
        }else if ("commentCount".equals(countName)){
            return publish.getCommentCount();
        }
        return 0;
    }
    public int findAndModifyVideo(ObjectId publishId,String countName,int inc){
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(publishId));
        Update update = new Update();
        update.inc(countName,inc);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Video video = mongoTemplate.findAndModify(query, update, options, Video.class);
        if ("likeCount".equals(countName)){
            return video.getLikeCount();
        }else if ("loveCount".equals(countName)){
            return video.getLoveCount();
        }else if ("commentCount".equals(countName)){
            return video.getCommentCount();
        }
        return 0;
    }
}
